package com.dg.cuckoo.admin.service.impl;

import com.dg.cuckoo.admin.dto.projectDeclare.oneProDeclare.BuildDeclareDto;
import com.dg.cuckoo.admin.repository.BuildDeclareRepository;
import com.dg.cuckoo.admin.service.StatisticsService;
import com.dg.cuckoo.common.utils.ListUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class StatisticsServiceimpl implements StatisticsService {

    @Autowired
    private BuildDeclareRepository buildDeclareRepository;

    @Override
    public List<BuildDeclareDto> buildQuery(String year, String unit) {
        List<BuildDeclareDto> dtos1 = buildDeclareRepository.findByBeginYear(year);
        List<BuildDeclareDto> dtos2 = buildDeclareRepository.findByUnit(unit);
        int number = number(year, unit);
        if(number>1){
            List<BuildDeclareDto> buildDeclareDtos = ListUtil.BuildDeclare(dtos1, dtos2);
            return buildDeclareDtos;
        }
        dtos1.addAll(dtos2);
        List<BuildDeclareDto> result = dtos1.stream().distinct().collect(Collectors.toList());
        return result;
    }

    public int number(String year,String unit){
        int number=0;
        if(year!=null&&!year.equals("")){
            number++;
        }
        if (unit!=null&&!unit.equals("")){
            number++;
        }
        return number;
    }
}
